package com.krtech.wecard.module.mayi.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.security.Inner;
import com.krtech.wecard.module.mayi.entity.MayiCdkMode;
import com.krtech.wecard.module.mayi.entity.MayiLiuliang;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.service.MayiLiuliangService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/mayiLiuliang")
@Slf4j
@Api(value = "蚂蚁阅读量流量管理")
public class MayiLiuliangController {

    @Autowired
    private MayiLiuliangService liuliangService;

    @GetMapping("/addMode")
    @ApiOperation(value = "添加模版")
    @Inner(value = false)
    public boolean backAdd(String code) {
        //查询是否有了
        int count = liuliangService.count(new QueryWrapper<MayiLiuliang>().eq(MayiLiuliang.COL_CODE, code));
        if (count > 0){
            throw new BusinessException(code+"，已经存在");
        }
        MayiLiuliang liuliang = new MayiLiuliang();
        liuliang.setCreatedTime(new Date());
        liuliang.setUpdatedTime(new Date());
        liuliang.setCode(code);

        return liuliangService.save(liuliang);
    }

    @GetMapping("/page")
    @ApiOperation(value = "page")
    public Page<MayiLiuliang> page(Page<MayiLiuliang> page,String code) {
        QueryWrapper<MayiLiuliang> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(code)){
            wrapper.like(MayiLiuliang.COL_CODE,code);
        }
        return liuliangService.page(page,wrapper);
    }
}
